//    Copyright (C) Kherty.  All rights reserved.
using System.Windows.Media;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing
{
    public class TextFont : IOfficeXmlSerializableAs
    {
        private string typeface;

        public string Typeface
        {
            get { return typeface; }
            set { typeface = value; }
        }

        #region IOfficeXmlSerializableAs Members

        void IOfficeXmlSerializableAs.ReadXmlAs(string name, ReadContext context)
        {
            using (var c = context.Read(name))
            {
                Typeface = c.GetOptionalString("typeface");
            }
        }

        void IOfficeXmlSerializableAs.WriteXmlAs(string name, WriteContext context)
        {
            using (WriteContext c = context.Write(name))
            {
                c.SetOptionalString("typeface", Typeface);
            }
        }

        #endregion

        public static FontFamily ReadFontfamily(string name, ReadContext context)
        {
            TextFont f = context.ReadElementAs<TextFont>(name);
            return new FontFamily(f.Typeface);
        }

        public static FontFamily ReadOptionalFontFamily(string name, ReadContext context)
        {
            if (context.ReaderLocalName != name)
                return null;
            return ReadFontfamily(name, context);
        }

        public static void WriteFontFamily(FontFamily ff, string name, WriteContext context)
        {
            TextFont f = new TextFont();
            f.Typeface = ff.Source;
            context.WriteElementAs(f, name);
        }


        internal static void WriteOptionalFontFamily(FontFamily font, string p, WriteContext c)
        {
            if (font == null)
                return;
            WriteFontFamily(font, p, c);
        }
    }
}